package com.taxshare.seller.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.CarProductionPlaceEntity;
import com.taxshare.seller.mapper.CarProductionPlaceMapper;
import com.taxshare.seller.view.dto.CarProductionPlaceDTO;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author cxl
 * @date 2021/8/19 11:55
 */
@Service
public class CarProductionPlaceService extends ServiceImpl<CarProductionPlaceMapper, CarProductionPlaceEntity> {

    /**
     * 车辆产地【车辆产地信息新增】
     *
     * @param placeDTO 新增参数
     * @return 结果
     */
    public Response addProductionPlace(CarProductionPlaceDTO placeDTO) {
        if (CollectionUtil.isNotEmpty(listProductionPlaceByName(placeDTO.getProductionPlaceName()))) {
            return Response.error("SEL_CAR_002");
        }
        CarProductionPlaceEntity placeEntity = new CarProductionPlaceEntity();
        BeanUtils.copyProperties(placeDTO, placeEntity);
        placeEntity.setCreateUserid(UserInfoUtil.getUserID());
        placeEntity.setCreateTime(new Date());
        placeEntity.setUuid(UUIDUitl.UUID32(false));
        this.save(placeEntity);
        return Response.success();
    }

    /**
     * 车辆产地【车辆产地信息删除】
     *
     * @param ids 主键ids
     * @return 结果
     */
    public Response removeProductionPlace(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.error("SEL_CAR_001");
        }
        this.removeByIds(ids);
        return Response.success();
    }

    public List<CarProductionPlaceEntity> listProductionPlaceByName(String name) {
        QueryWrapper<CarProductionPlaceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("production_place_name", name);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 车辆产地【车辆产地信息编辑】
     *
     * @param placeDTO 参数
     * @return 结果
     */
    public Response editProductionPlace(CarProductionPlaceDTO placeDTO) {
        if(StringUtils.isEmpty(placeDTO.getUuid())){
            return Response.error("SEL_CAR_001");
        }
        CarProductionPlaceEntity byId = this.getById(placeDTO.getUuid());
        List<CarProductionPlaceEntity> placeEntities = listProductionPlaceByName(placeDTO.getProductionPlaceName());
        if (!StringUtils.equals(byId.getProductionPlaceName(), placeDTO.getProductionPlaceName())
                && !CollectionUtils.isEmpty(placeEntities)) {
            return Response.error("SEL_CAR_002");
        }
        CarProductionPlaceEntity placeEntity = new CarProductionPlaceEntity();
        BeanUtils.copyProperties(placeDTO, placeEntity);
        placeEntity.setUpdateUserid(UserInfoUtil.getUserID());
        placeEntity.setUpdateTime(new Date());
        this.updateById(placeEntity);
        return Response.success();
    }

    /**
     * 车辆产地【车辆产地信息查询】
     *
     * @param keyword 关键词
     * @return 列表
     */
    public Response listProductionPlaceByKeyword(String keyword) {
        QueryWrapper<CarProductionPlaceEntity> queryWrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.and(wrapper -> wrapper.like("production_place_name", keyword)
                    .or().like("production_place_code", keyword).or().like("production_place_abbreviation", keyword));

        }
        queryWrapper.orderByDesc("create_time");
        return Response.success(this.list(queryWrapper));
    }

}
